<template>
	<el-dialog title="会签意见设置" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="会签意见" prop="ideaText" maxlength="50">
				<el-input v-model="form.ideaText" placeholder="会签意见" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否启用">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<div style="margin: 10px;text-align: center;">
				<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			</div>
			<el-table :data="commIdeaList" border stripe style="width: 100%" class="customer-table">
				<el-table-column prop="sortNo" width="50" label="#"></el-table-column>
				<el-table-column prop="ideaText" width="350" align="center" label="会签意见"></el-table-column>
				<el-table-column prop="status" align="center" width="80" fixed="right" label="状态">
					<template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="80">
					<template #default="scope">
						<el-button-group>
							<el-popconfirm title="确定删除吗？" @confirm="deleteCommIdea(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>
		</el-form>
	</el-dialog>
</template>

<script>

export default {
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			commIdeaList: [],
			form: {
				sortNo:1,
				ideaId: "",
				ideaText: "",
				status:'1'
			},
			rules: {
				ideaText: [
					{required: true, message: '会签意见不能为空'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {
		this.getAllBpmCommIdeaList();
	},
	methods: {
		//显示
		open() {
			this.visible = true;
			return this;
		},
		async changeSwitch(val, row)
		{
			row.status = row.status == '1' ? '0' : '1'
			row.$switch_status = true;
			delete row.$switch_status;
			row.status = val;
			var res = await this.$API.bpm.bpmCommIdea.update.post({ideaId: row.ideaId, status: row.status});
			if (res.code == 200) {
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getAllBpmCommIdeaList() {
			var res = await this.$API.bpm.bpmCommIdea.getAllBpmCommIdeaList.get();
			if (res.code == 200) {
				this.commIdeaList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async deleteCommIdea(row, index) {
			this.$confirm(`确定删除选中的会签意见吗？如果删除项中含有子集将会被一并删除`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var res = await this.$API.bpm.bpmCommIdea.delete.post({ideaId: row.ideaId});
				if (res.code == 200) {
					this.getAllBpmCommIdeaList();
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
						this.isSaveing = true;
						var res = await this.$API.bpm.bpmCommIdea.insert.post(this.form);
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.ideaId = data.ideaId
			this.form.ideaCommIdeaText = data.ideaCommIdeaText
		}
	}
}
</script>

<style>
</style>
